"""
-*- coding: utf-8 -*-
@Time : 2022/5/18 19:09
@Author : XGT-Python
"""
from request_util import WebRequest
from config import logging
import re


def kuai_da_li():
    req = WebRequest()
    for i in range(1, 6):
        url = f'https://free.kuaidaili.com/free/inha/{i}/'
        req.do_get(url)
        dom = req.get_tree()
        ips = dom.xpath('//tr/td[1]/text()')
        ports = dom.xpath('//tr/td[2]/text()')
        _types = dom.xpath('//tr/td[4]/text()')

        for i, p, t in zip(ips, ports, _types):
            # logging.info(f'{t.lower()}://{i}:{p}')
            yield {'ip': i, 'port': p, '_type': t.lower(), 'full_url': f'{t.lower()}://{i}:{p}'}


def yun_dai_li():
    req = WebRequest()
    for i in range(1, 6):
        url = f'http://www.ip3366.net/?stype=1&page={i}'
        req.do_get(url)
        dom = req.get_tree()
        ips = dom.xpath('//tr/td[1]/text()')
        ports = dom.xpath('//tr/td[2]/text()')
        _types = dom.xpath('//tr/td[4]/text()')

        for i, p, t in zip(ips, ports, _types):
            # logging.info(f'{t.lower()}://{i}:{p}')
            yield {'ip': i, 'port': p, '_type': t.lower(), 'full_url': f'{t.lower()}://{i}:{p}'}

def all_word():
    url = 'https://ip.jiangxianli.com/?page=1'
    req = WebRequest()
    req.do_get(url)
    text = req.get_text()
    count,limit = re.findall('count: "(\d+)",limit: "(\d+)"',text)[0]
    page = (int(count // limit)) + 1
    for i in range(1, page):
        url = f'https://ip.jiangxianli.com/?page={i}'
        req.do_get(url)
        dom = req.get_tree()
        ips = dom.xpath('//tr/td[1]/text()')
        ports = dom.xpath('//tr/td[2]/text()')
        _types = dom.xpath('//tr/td[4]/text()')
        full_url = dom.xapth('//tr/td[last()]/button[1]/@data-url')
        for i, p, t,f in zip(ips, ports, _types,full_url):
            logging.info(f'{t.lower()}://{i}:{p}')
            yield {'ip': i, 'port': p, '_type': t.lower(), 'full_url': f}



if __name__ == '__main__':
    # kuai_da_li()
    # yun_dai_li()
    all_word()

